package com.wowo.aop.transaction;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class App2 {

    public static void main(String[] args) {
        StudentService studentService2 = new StudentServiceImpl();
        TransactionApsect transactionApsect = new TransactionApsect();
        //studentService.save();
        InvocationHandler h = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("in invoke");
                String name = method.getName();
                System.out.println(name+" , "+args);

                if (args !=null){
                    System.out.println(args[0]);
                }
                Object result = null;
                transactionApsect.start();
                try {
                    result= method.invoke(studentService2, args);
                }catch (Exception e){
                    transactionApsect.rollback();
                }
                transactionApsect.commit();
                return result;

            }
        };
        //1，ClassLoader对象：App2的ClassLoader对象
        //2.动态代理类所实现的接口，实现接口中的方法
        //3.方法的实现在invoke方法的实现
        StudentService studentService =(StudentService) Proxy.newProxyInstance(
                App2.class.getClassLoader(),new Class[]{StudentService.class},h);
        System.out.println(App2.class.getClassLoader());

        studentService.save();
        studentService.update();
        studentService.delete(23);
    }
}
